/**
 * Created by LeonTao on 17/2/21.
 */

//营业执照

$('#license-upload-wrap').bind('dragover', function () {
    $('#license-upload-wrap').addClass('image-dropping');
});
$('#license-upload-wrap').bind('dragleave', function () {
    $('#license-upload-wrap').removeClass('image-dropping');
});

$('#license-upload-btn').click(function () {
    $(this).text('请稍等...');
    $('#license-upload-input').trigger('click');
});

$('#license-upload-input').on('change', function () {
    console.log('--- license upload input ---');
    readLicense(this);
});

function readLicense(input) {
    if (input.files && input.files[0]) {

        var reader = new FileReader();

        reader.onload = function (e) {
            $('#license-upload-wrap').hide();
            $('#license-upload-image').attr('src', e.target.result);
            $('#license-upload-content').show();

            $('#license-title').html(input.files[0].name);
            $('#license-upload-btn').text('更换图片');
        };

        reader.readAsDataURL(input.files[0]);

    } else {
        removeLicense();
    }
}

function removeLicense() {
    $('#license-upload-input').replaceWith($('#license-upload-input').clone());
    $('#license-upload-content').hide();
    $('#license-upload-wrap').show();
}


//组织机构代码扫描件

//点击上次组织机构代码扫描件按钮
$('#code-upload-btn').click(function () {
    $(this).text('请稍等...');
    $('#code-upload-input').trigger('click');
});

$('#code-upload-input').on('change', function () {
    console.log('--- code upload input ---');
    readCode(this);
});


$('#code-upload-wrap').bind('dragover', function () {
    $('#code-upload-wrap').addClass('image-dropping');
});
$('#code-upload-wrap').bind('dragleave', function () {
    $('#code-upload-wrap').removeClass('image-dropping');
});

function readCode(input) {
    console.log('--- readCode ---', input);
    if (input.files && input.files[0]) {

        var reader = new FileReader();

        reader.onload = function (e) {
            $('#code-upload-wrap').hide();

            $('#code-upload-image').attr('src', e.target.result);
            $('#code-upload-content').show();

            $('#code-title').html(input.files[0].name);

            $('#code-upload-btn').text('更换图片');
        };

        reader.readAsDataURL(input.files[0]);

    } else {
        removeCode();
    }
}

function removeCode() {
    $('#code-upload-input').replaceWith($('#code-upload-input').clone());
    $('#code-upload-content').hide();
    $('#code-upload-wrap').show();
}
